package com.gosophia.govDoc.action.transientDoc;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.govDoc.dto.TransientDocumentDTO;
import com.gosophia.govDoc.ws.TransientDocumentWebService;
import com.gosophia.springsecurity.UserDetailsImpl;

/**
 * 公文起草：移除公文Action
 * @author zhuanhuanchao
 * @since 2011-01-25
 */
@Controller("RemoveTransientDocumentAction")
@Scope("prototype")
@SuppressWarnings("serial")
public class RemoveTransientDocumentAction extends BaseSupportAction {

	private Long jobId;
	private boolean hasTransientDoc;
	private final Logger logger =Logger.getLogger(this.getClass());
	
	private TransientDocumentDTO transientDocumentDTO;
	@Autowired
	private TransientDocumentWebService transientDocumentWebService;

	@Override
    public String _execute() throws Exception {
		
		//得到当前用户
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
		.getContext().getAuthentication().getPrincipal();
		Long creatorId = userDetails.getUserAccountDTO().getUserInfoDTO().getUserInfoId();
		logger.debug("删除该用户的临时文件,creatorId:=>"+creatorId+ "   jobId=>"+jobId );
		//查询当前用户或者任务ID所起草的公文
		transientDocumentDTO = transientDocumentWebService.findTranDocByCreatorAndJobId(creatorId, jobId); 
		if(transientDocumentDTO!=null) {
			//移除当前用户或者任务ID所起草的公文
			transientDocumentWebService.removeTransientDocument(transientDocumentDTO);
		}
		 logger.debug("删除该用户的临时文件成功" );
		transientDocumentDTO=null;//用户临时公文被置空
		return SUCCESS;
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

    public TransientDocumentDTO getTransientDocumentDTO() {
        return transientDocumentDTO;
    }

    public void setTransientDocumentDTO(TransientDocumentDTO transientDocumentDTO) {
        this.transientDocumentDTO = transientDocumentDTO;
    }

    public boolean isHasTransientDoc() {
        return hasTransientDoc;
    }

    public void setHasTransientDoc(boolean hasTransientDoc) {
        this.hasTransientDoc = hasTransientDoc;
    }

}
